package cn.dengta.webapp.user.vo;

import java.util.Date;
import java.util.Map;
import javax.annotation.Nullable;

import cn.dengta.common.model.APIResponse.Field;
import cn.dengta.common.model.Status;
import cn.dengta.common.util.Formats;
import cn.dengta.context.model.Language;
import cn.dengta.webapp.base.entity.I18nContent;
import cn.dengta.webapp.base.vo.AbstractVO;
import cn.dengta.webapp.user.entity.Tag;
import cn.dengta.webapp.user.entity.Tag.TagType;
import cn.dengta.webapp.user.model.Gender;
import cn.dengta.webapp.user.model.Global;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.*;

/**
 * 礼物
 */
@Setter
@Getter
@Accessors(chain = true)
public class TagVO extends AbstractVO<Tag> {

	@Field(name = "名称")
	private Object name;
	@Field(name = "类型")
	private TagType type;
	/** 子类型 SubTagType */
	private Integer subType;
	@Field(name = "摘要")
	private String summary;
	@Field(name = "图标")
	private String image;
	@Field(name = "排序")
	private Integer sort;
	@Field(name = "状态")
	private Integer status;
	@Field(name = "标签数")
	private Integer totalCount;
	@Field(name = "性别")
	public Gender gender;
	@Field(name = "范围")
	private Global global;
	/** 所属分包 */
	@Field(name = "分包")
	private String wesKits;
	@Field(name = "开启时间")
	/** 使用数 */
	private Long useCount;
	private transient Date openTime;
	protected transient Map<Language, String> dataMap;

	public static String[] frontList = { "status", "type", "addTime" };

	public static TagVO from(@Nullable Tag source) {
		TagVO vo = copy(source, TagVO::new);
		if (vo != null && source.getType().i18n) {
			vo.name = X.map(source.getResolvedLanguageCfg(), I18nContent::getDataMap);
		}
		return vo;
	}

	public static TagVO fromFront(@Nullable Tag source) {
		TagVO vo = copy(source, TagVO::new, frontList);
		if (vo != null && source.getType().i18n) {
			vo.name = StringUtil.toString(X.map(source, Tag::i18nName));
			vo.dataMap = X.map(source.getResolvedLanguageCfg(), I18nContent::getDataMap);
		}
		return vo;
	}

	public Object i18nName(Language language) {
		Object i18nName = dataMap != null ? dataMap.get(language) : this.name;
		if (i18nName == null && dataMap != null) {
			i18nName = dataMap.get(Language.en);
		}
		return name = i18nName;
	}

	public Integer getType() {
		return X.map(type, TagType::getValue);
	}

	public String getType_() {
		return X.map(type, TagType::getLabel);
	}

	public String getAddTime_() {
		return super.getAddTime_();
	}

	public String getStatus_() {
		return X.map(status, s -> Status.of(s).getToggleLabel());
	}

	public String getOpenTime_() {
		return Formats.formatDefDate(openTime);
	}

	public Boolean isOpen(Long baseTime) {
		if (baseTime == null) {
			baseTime = now().getTime();
		}
		return openTime == null || baseTime >= openTime.getTime();
	}

	public Integer getGender() {
		return X.map(gender, Gender::getValue);
	}

	public String getGender_() {
		return X.map(gender, Gender::getLabel);
	}

	public Integer getGlobal() {
		return X.map(global, Global::getValue);
	}

	public String getGlobal_() {
		return X.map(global, Global::getLabel);
	}


	public String getSubType_() {
		return this.subType == null ? getType_() : X.map(Tag.SubTagType.of(this.subType), Tag.SubTagType::getLabel);
	}

	public Long getUseCount() {
		return NumberUtil.getLong(useCount, 0) * 122;
	}

}
